package com.hejiale.lease.common.sms;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.hejiale.lease.common.exception.LeaseException;
import com.hejiale.lease.common.result.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: 短信配置类
 * @author: HeJiale
 * @since 2025/3/22 下午2:17
 */
@Configuration
@EnableConfigurationProperties(AliyunSMSProperties.class)
@ConditionalOnProperty(name = "aliyun.sms.endpoint")
public class AliyunSMSConfiguration {
    private final AliyunSMSProperties aliyunSMSProperties;

    @Autowired
    public AliyunSMSConfiguration(AliyunSMSProperties aliyunSMSProperties) {
        this.aliyunSMSProperties = aliyunSMSProperties;
    }

    @Bean
    public Client smsClient(){
        Config config = new Config();
        config.setAccessKeyId(aliyunSMSProperties.getAccessKeyId())
                .setAccessKeySecret(aliyunSMSProperties.getAccessKeySecret())
                .setEndpoint(aliyunSMSProperties.getEndpoint());
        try {
            return new Client(config);
        } catch (Exception e) {
            throw new LeaseException(ResultCodeEnum.SERVICE_ERROR);
        }
    }
}
